<?php
class Shop_OrderController extends Promotor_Controller_Action_Scaffold {
	public $skin = array(
		// forsuje domyslny template i style na admin
		'template' => 'admin',
		'style' => 'default',
		// layout
		'layout' => 'admin_shop',
		// rozszerzam konfiguracje
		'config' => array(
			'filename' => 'backend_config.ini'
		)
	);

	public $acl = array(
		'list' => 'list',
		'info' => 'list',
		'add' => 'create',
		'edit' => 'update',
		'delete' => 'delete',
		'get' => 'update',
		'confirm' => 'update',
		'cancel' => 'update',
	);

	/**
	 * @see Promotor_Controller_Action_Scaffold specialization
	 */
	protected $_modelClass = 'Shop_Model_Order';
//	protected $_formAddClass = 'Shop_Form_Attr_Add';
//	protected $_formEditClass = 'Shop_Form_Attr_Edit';
	protected $_formRemoveClass = 'Shop_Form_Order_Remove';
	// end of specialization
	
	/**
	 * @return void
	 */
	public function indexAction() 
	{
		$this->_forward('list');
	}
	
	/**
	 * @return void
	 */
	public function infoAction() 
	{
		$model = new Shop_Model_Order();
		$this->view->row = $model->findByPK($this->_getParam('id'));
		$this->_helper->redirectUntil->gotoUrlUntil($this->view->row, getenv('HTTP_REFERER'));
	}

	/**
	 * Pobranie statusu transakcji
	 */
	public function getAction()
	{
		/* @var $flashMessanger Zend_Controller_Action_Helper_FlashMessenger */
		$flashMessanger = $this->_helper->getHelper('flashMessenger');

		$model = new Shop_Model_Order();
		$row = $model->findByPK($this->_getParam('id'));

		if (!$row instanceof Zend_Db_Table_Row_Abstract)
		{
			$flashMessanger->addMessage('Rekord nie istnieje');
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}

		$config = $this->_helper->config('config.ini','payments');
		
		try {
			$payments = new KontorX_Payments_Platnosci($config);
			$payments->setSessionId($row->session_id);
			$response = $payments->request(KontorX_Payments_Platnosci::ACTION_GET);
		} catch(Exception $e) {
			$flashMessanger->addMessage($e->getMessage());
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}
		
		if ($response->getStatus(false) != 'OK')
		{
			$flashMessanger->addMessage($response->getTransStatus(true));
			$flashMessanger->addMessage('Nie udało sie sprawdzić statusu transakcji: "'.$message.'" ['.$nr.']');
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}
		
		$row->status = $response->getTransStatus();
		
		try {
			$row->save();
		} catch(Zend_Db_Exception $e) {
			$flashMessanger->addMessage($e->getMessage());
		}
		
		$flashMessanger->addMessage('Status transakcji: '. $response->getTransStatus(true));
		$flashMessanger->addMessage('Kwota transakcji: '. ($response->getAmount()/100) . ' zł');
		
		$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
	}

	/**
	 * Potwierdzenie płatności
	 * 
	 * TODO: Sprawdzenie czy status ulega zmianie
	 */
	public function confirmAction()
	{
		/* @var $flashMessanger Zend_Controller_Action_Helper_FlashMessenger */
		$flashMessanger = $this->_helper->getHelper('flashMessenger');

		$model = new Shop_Model_Order();
		$row = $model->findByPK($this->_getParam('id'));

		if (!$row instanceof Zend_Db_Table_Row_Abstract)
		{
			$flashMessanger->addMessage('Rekord nie istnieje');
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}

		$config = $this->_helper->config('config.ini','payments');
		
		try {
			$payments = new KontorX_Payments_Platnosci($config);
			$payments->setSessionId($row->session_id);
			$response = $payments->request(KontorX_Payments_Platnosci::ACTION_CONFIRM);
		} catch(Exception $e) {
			$flashMessanger->addMessage($e->getMessage());
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}

		if ($response->getStatus(false) != 'OK')
		{
			list ($nr, $message) = $response->getStatusError();
			$flashMessanger->addMessage('Nie udało sie potwierdzić transakcji: "'.$message.'" ['.$nr.']');
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}
		
		$row->status = $response->getTransStatus();
		
		try {
			$row->save();
		} catch(Zend_Db_Exception $e) {
			$flashMessanger->addMessage($e->getMessage());
		}
		
		$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
	}

	/**
	 * Anulowanie transakcji w platnosci.pl
	 * TODO: Sprawdzenie czy status ulega zmianie
	 * TODO: Usunięcie zamówienia
	 */
	public function cancelAction()
	{
		/* @var $flashMessanger Zend_Controller_Action_Helper_FlashMessenger */
		$flashMessanger = $this->_helper->getHelper('flashMessenger');

		$model = new Shop_Model_Order();
		$row = $model->findByPK($this->_getParam('id'));

		if (!$row instanceof Zend_Db_Table_Row_Abstract)
		{
			$flashMessanger->addMessage('Rekord nie istnieje');
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}

		$config = $this->_helper->config('config.ini','payments');
		try {
			$payments = new KontorX_Payments_Platnosci($config);
			$payments->setSessionId($row->session_id);
			$response = $payments->request(KontorX_Payments_Platnosci::ACTION_CANCEL);
		} catch(Exception $e) {
			$flashMessanger->addMessage($e->getMessage());
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}

		if ($response->getStatus(false) != 'OK')
		{
			list ($nr, $message) = $response->getStatusError();
			$flashMessanger->addMessage('Nie udało sie anulować transakcji: "'.$message.'" ['.$nr.']');
			$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
		}
		
		$row->status = $response->getTransStatus();
		
		try {
			$row->save();
		} catch(Zend_Db_Exception $e) {
			$flashMessanger->addMessage($e->getMessage());
		}
		
		$this->_helper->redirector->gotoUrlAndExit(getenv('HTTP_REFERER'));
	}
}